<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\InvestOrg
 *
 * @property integer $id
 * @property string $invest_name
 * @property integer $img_logo_id
 * @property integer $link_web_id
 * @property string $invest_desc
 * @property string $invest_phase
 * @property string $invest_field
 * @property string $invest_money
 * @property integer $min_quota
 * @property integer $max_quota
 * @property string $wechat
 * @property string $weibo
 * @property string $begin_date
 * @property integer $user_id
 * @property string $deleted_at
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Investor[] $investor
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\InvestLog[] $investLog
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\User[] $userFocusInvestOrg
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Tags[] $investorg_x_tags
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Fields[] $investorg_x_fields
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Phases[] $investorg_x_phase
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereInvestName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereImgLogoId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereLinkWebId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereInvestDesc($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereInvestPhase($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereInvestField($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereInvestMoney($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereMinQuota($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereMaxQuota($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereWechat($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereWeibo($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereBeginDate($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereUserId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereUpdatedAt($value)
 * @property-read \App\Images $toOrgLogo
 * @property-read \App\ProjectUrl $toLinkWeb
 * @property integer $status
 * @method static \Illuminate\Database\Query\Builder|\App\InvestOrg whereStatus($value)
 */
class InvestOrg extends Model {

    //表名
    protected  $table = 'invest_org' ;
    use SoftDeletes;

    /** 关联投资人信息表 */
    public function investor()
    {
        return $this->hasMany('App\Investor','org_id');
    }

    /** 关联投资历程表 */
    public function investLog()
    {
        return $this->hasOne('App\InvestLog', 'org_id');
    }

    /** 关联中间表-用户关注投资机构 */
    public function userFocusInvestOrg()
    {
        return $this->belongsToMany('App\User','focus_invest_org','org_id','user_id');
    }


    /**
     * 投资机构logo的获取
     */
    public function toOrgLogo()
    {
        return $this->belongsTo('App\Images','img_logo_id');
    }

    /** 投资机构链接 */
    public function toLinkWeb()
    {
        return $this->belongsTo('App\ProjectUrl' , 'link_web_id');
    }


    /** 关联中间表-投资机构标签 */
    public function investorg_x_tags(){
        return $this->belongsToMany('App\Tags','tags_relations','org_id','tag_id');
    }

    /** 关联投资机构-投资领域 */
    public function investorg_x_fields()
    {
        return $this->belongsToMany('App\Fields' , 'invest_fields' , 'org_id' , 'field_id');
    }

    /** 关联投资机构-投资阶段 */
    public function investorg_x_phase()
    {
        return $this->belongsToMany('App\Phases' , 'phase_relations' , 'org_id' , 'phase_id');
    }

    /**
     * 通过投资机构标识获取投资阶段
     * @param $orgId
     * @return string
     */
    public function getFieldsByOrgId($orgId)
    {
        $investOrg = InvestOrg::find($orgId)->investorg_x_fields()->getResults();
        $fields = array();
        if($investOrg) {
            foreach($investOrg as $key=> $orgs){
                $fields[] = $orgs['field'];
            }
        }
        return $fields;
    }

    /**
     * 通过机构标识获取投资阶段
     * @param $orgId
     * @return array
     */
    public function getPhasesByOrgId($orgId)
    {
        $investOrg = InvestOrg::find($orgId)->investorg_x_phase()->getResults();
        $phases = array() ;
        if($investOrg){
            foreach($investOrg as $key=> $orgs){
                $phases[] = $orgs['phase'];
            }
        }
        return $phases;
    }

    /**
     * 通过投资机构标识获取项目个数
     * @param $orgId
     * @return int
     */
    public function getProjectCounts($orgId)
    {
        $projectIds = InvestLog::whereOrgId($orgId)->lists('project_id');
        $counts = 0;
        if($projectIds)
            $counts = count($projectIds);
        return $counts;
    }

    /**
     * 通过投资机构标识获取机构成员
     * @param $orgId
     * @return string
     */
    public  function getOrgMemberByOrgId($orgId)
    {
        $userIds = Investor::whereOrgId($orgId)->lists('user_id');
        $users = array();
        if($userIds) {
            $users = array();
            foreach ($userIds as $id) {
                if($user = UserInfo::whereUserId($id)->first())
                   $users[]=$user;
            }
        }
        return $users;
    }

    /**
     * 通过投资机构标识获取项目
     * @param $orgId
     * @return array
     */
    public function getProjectNameByOrgId($orgId)
    {
        $projectIds = InvestLog::whereOrgId($orgId)->lists('project_id');
        $projects = array();
        if($projectIds){
            foreach($projectIds as $id){
                $project = Project::whereId($id)->pluck('project_name');
                if($project)
                    $projects[] = $project;
            }

        }
        return $projects;

    }

    /**
     * 投过投资机构标志获取项目信息
     * @param $orgId
     * @return array
     */
    public function getProjectByOrgId($orgId)
    {
        $projectIds = InvestLog::whereOrgId($orgId)->lists('project_id');
        $projects = array();
        if($projectIds){
            foreach($projectIds as $projectId){
                $project = Project::whereId($projectId)->first();
                if($project)
                    $projects[] = $project ;
            }
        }
        return $projects;
    }

    /**
     * 通过用户标识获取投资机构成员信息
     * @param $userId
     * @return array|mixed|static
     */
    public function getUserInfoByUserId($userId)
    {
        $user = UserInfo::whereUserId($userId)->first();
        if($user)
            return $user;
        else
            return array();
    }

    /**
     * 判断当前投资机构是否已存在该标签
     * @param $investorgId
     * @return mixed
     */
    public function isExistsTags($investorgId)
    {
        return $this->investorg_x_tags()->wherePivot('org_id','=',$investorgId)->exists();
    }


}
